﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HandleExcel
{
    public class Program
    {
        static void Main(string[] args)
        {

            #region 单例模式
            Process[] allProcess = Process.GetProcesses();
            List<Process> prolist = new List<Process>();
            foreach (Process p in allProcess)
            {
                if (p.ProcessName.ToLower() == "handleexcel")
                {
                    Console.WriteLine(p.StartTime.ToString() + p.ProcessName);
                    prolist.Add(p);
                }
            }
            Console.WriteLine("当前启动程序进程数：" + prolist.Count);
            if (prolist.Count > 1)
            {
                for (int m = 0; m < prolist.Count - 1; m++)
                {
                    try
                    {
                        for (int i = 0; i < prolist[m].Threads.Count; i++)
                            prolist[m].Threads[i].Dispose();
                        prolist[m].Kill();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                        continue;
                    }
                }
            }
            Console.WriteLine("启动中~~~请稍等~~");
            System.Threading.Thread.Sleep(10000);

            Process[] allProcess1 = Process.GetProcesses();
            List<Process> prolist1 = new List<Process>();
            foreach (Process p1 in allProcess1)
            {
                if (p1.ProcessName.ToLower() == "handleexcel")
                {
                    Console.WriteLine(p1.StartTime.ToString() + p1.ProcessName);
                    prolist1.Add(p1);
                }
            }
            Console.WriteLine("进程杀死后，当前启动程序进程数：" + prolist1.Count);
            Console.WriteLine("线程开始 " + DateTime.Now.ToString());
            Console.WriteLine("开始删除无用的日志文件完成" + DateTime.Now.ToString());
            Log.DelateLog();
            Console.WriteLine("删除无用的日志文件完成" + DateTime.Now.ToString());
            #endregion



            try
            {
                //执行主业务逻辑
                HandleExcel.HandleExcelContent();

            }
            catch (Exception exception)
            {
                //任务失败，重启程序
                Log.WriteLog("run", exception.ToString());
                DateTime now = DateTime.Now;
                int SleepTime = int.Parse(ConfigurationManager.AppSettings["SleepTime"].ToString());
                Log.WriteLog("run", "替换出现异常，开始休眠" + SleepTime + "分钟.............." + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                while (DateTime.Now.AddMilliseconds(-60000.0 * SleepTime).CompareTo(now) <= 0)
                {
                }
                try
                {
                    Process.Start("handleexcel");
                }
                catch (Exception ex)
                {
                    Log.WriteLog("run", ex.ToString());
                }
            }


        }
    }
}
